package com.msu.nava.util;

import com.msu.nava.modle.ViewObject;

import java.util.concurrent.TimeUnit;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class HttpUtil {


  /**
   * @param url      请求地址
   * @param illname  用户需要查询的病症名
   * @param callback
   * @return response中返回 问卷,可选症状的列表,List<SickdetailItem>
   */
  public static void sendQuestionnaireRequest(String url, String illname, okhttp3.Callback callback) {
    //使用post方法
    //questionnaire
    OkHttpClient client = new OkHttpClient();
    RequestBody requestBodyPost = new FormBody.Builder()
      .add("illname", illname)
      .build();

    Request request = new Request.Builder()
      .url(url)
      .post(requestBodyPost)
      .build();
    client.newCall(request).enqueue(callback);
  }
  public static void loginRequest(String url, String username,String password, okhttp3.Callback callback){
    OkHttpClient client = new OkHttpClient();
    RequestBody requestBodyPost = new FormBody.Builder()
      .add("username", username)
      .add("password",password)
      .build();

    Request request = new Request.Builder()
      .url(url)
      .post(requestBodyPost)
      .build();
    client.newCall(request).enqueue(callback);
  }

  /**
   * @param url             请求地址
   * @param userAnswerSheet 用户选择结果，包含illname和list<Interger>sdids,是ViewObject的json序列化对象
   * @param callback
   * @retrun response中返回药物推荐表
   */
  public static void sendRecipeRequest(String url, String userAnswerSheet, okhttp3.Callback callback) {
    OkHttpClient client = new OkHttpClient();
    RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8")
      , userAnswerSheet);
    Request request = new Request.Builder()
      .url(url)
      .post(requestBody)
      .build();
    client.newCall(request).enqueue(callback);
  }
  public static void getOrferInfoFromServer(String url, okhttp3.Callback callback){
    OkHttpClient client = new OkHttpClient.Builder()
      .connectTimeout(10, TimeUnit.SECONDS)
      .readTimeout(20, TimeUnit.SECONDS)
      .build();
    client.connectTimeoutMillis();
    Request request = new Request.Builder()
      .url(url)
      .build();
    client.newCall(request).enqueue(callback);
  }

}
